﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using LinQ_DTO;

using System.Xml;
namespace TemplateH4
{
    public partial class GioHang : System.Web.UI.Page
    {
        //double ThanhTien = 0;
        //double TongTien = 0;        //chua tinh diem tich luy
        //double TienGiamDiemTichLuy = 0;        
        protected void Page_PreInit(object sender, EventArgs e)
        {
            String thm;
            thm = (string)Session["themeSelected"];
            if (thm != null)
            {
                Page.Theme = thm;
            }
            else
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(Server.MapPath("~/config.xml"));
                XmlNode root = doc.DocumentElement;
                String theme = root.SelectSingleNode("theme").ChildNodes[0].Value.Trim();
                Session.Add("themeSelected", theme);
                Page.Theme = theme;
            }

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
                BindData();

        }

        private void BindData()
        {
            
            List<shop_sanpham> GioHang = (List<shop_sanpham>)Session["GioHang"];
            List<SanPhamTrangGioHang> GioHang1 = new List<SanPhamTrangGioHang>();
            if (GioHang == null)
            {
                lbThongBao1.Text = "Không có sản phẩm nào trong giỏ hàng. Xin quay lại trang ";
                hl2SanPham.Text = "Sản Phẩm ";
                lbThongBao2.Text = "để chọn hàng.";
                this.pnThongBao.Visible = true;
                UpdatePanelGrvSP.Visible = false;
                return;
            }
            

            //foreach (shop_sanpham Sp in GioHang)
            //{
            //    SanPhamTrangGioHang Sp1 = new SanPhamTrangGioHang();

            //    Sp1.TenLoaiSanPham = Sp.shop_loaisanpham.TenLoaiSanPham;
            //    Sp1.DonGia = Sp.DonGia;
            //    Sp1.Phai = Sp.Phai == true ? "Nam" : "Nữ";
            //    Sp1.TenNhaSanXuat = Sp.shop_nhasanxuat.TenNhaSanXuat;
            //    Sp1.TenSanPham = Sp.TenSanPham;

            //    string NoiDungKM = string.Empty;
            //    foreach (shop_sanpham_khuyenmai Km in Sp.shop_sanpham_khuyenmais)
            //    {
            //        NoiDungKM += Km.shop_khuyenmai.NoiDung + " / ";
            //    }
            //    if (NoiDungKM == "")
            //        Sp1.KhuyenMai = "Không";
            //    else
            //        Sp1.KhuyenMai = NoiDungKM;

            //    GioHang1.Add(Sp1);
            //}
            pnThongBao.Visible = false;
            UpdatePanelGrvSP.Visible = true;
            grvGioHang.DataSource = GioHang;
            grvGioHang.DataBind();

            
            double TongCong = this.TinhTien(GioHang) - this.TinhTien(GioHang)*GetCutDownRateFromPoint();
            this.lbTongCong.Text = string.Format("{0:0,0} VND",TongCong);
        }

        protected void CheckAll_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox CheckAll = (CheckBox) sender; 
            foreach (GridViewRow row in grvGioHang.Rows) 
             {
                 CheckBox cb = (CheckBox)row.FindControl("CheckProduct"); 
                 if (cb != null) 
                     cb.Checked = CheckAll.Checked; 
             } 
        }

        protected void btnThanhToan_Click(object sender, EventArgs e)
        {
            List<shop_sanpham> GioHang = (List<shop_sanpham>)Session["GioHang"];
            List<int> SelectedIndex = new List<int>();
            List<shop_sanpham> ListThanhToan = new List<shop_sanpham>();

            foreach (GridViewRow row in grvGioHang.Rows)
            {
                CheckBox cb = (CheckBox)row.FindControl("CheckProduct");
                if (cb != null && cb.Checked == true)
                {
                    SelectedIndex.Add(row.DataItemIndex);
                }
            }

            for (int i = SelectedIndex.Count - 1; i > -1; i--)
            {
                shop_sanpham Sp = GioHang[SelectedIndex[i]];
                ListThanhToan.Add(Sp);
            }
            
            Session["ThanhToan"] = ListThanhToan;
            Response.Redirect("~/ThanhToan.aspx");
        
        }

        protected void btnXoa_Click(object sender, EventArgs e)
        {
            List<shop_sanpham> GioHang = (List<shop_sanpham>)Session["GioHang"];
            List<int> SelectedIndex = new List<int>();
            List<shop_sanpham> ListThanhToan = new List<shop_sanpham>();

            foreach (GridViewRow row in grvGioHang.Rows)
            {
                CheckBox cb = (CheckBox)row.FindControl("CheckProduct");
                if (cb != null && cb.Checked == true)
                {
                    SelectedIndex.Add(row.DataItemIndex);
                }
            }

            for (int i = SelectedIndex.Count - 1; i > -1; i--)
            {
                GioHang.RemoveAt(i);    
            }
            
            Session["GioHang"] = GioHang;
            BindData();
 

        }

        protected void grvGioHang_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                shop_sanpham Sp = (shop_sanpham)e.Row.DataItem;//(shop_sanpham)ListSP[e.Row.];

                //bind image
                Image ImgControl = (Image)e.Row.FindControl("imgSP");
                ImgControl.AlternateText = Sp.HinhSanPham.Element("HinhTruoc").Value;
                ImgControl.ImageUrl = Sp.HinhSanPham.Element("HinhTruoc").Value;

               
               

                Label KM = (Label)e.Row.FindControl("lbKM");
                string NoiDungKM = string.Empty;
                foreach (shop_sanpham_khuyenmai Km in Sp.shop_sanpham_khuyenmais)
                {
                    NoiDungKM += Km.shop_khuyenmai.NoiDung;
                }
                if (NoiDungKM == "")
                    KM.Text = "Không";
                else
                    KM.Text = NoiDungKM;
                
                Label label;
                label = (Label)e.Row.FindControl("lbSL");
                label.Text = Sp.SoLuongNhap.ToString();
                
                label = (Label)e.Row.FindControl("lbSize");
                label.Text = Sp.Size.ToString();


                label = (Label) e.Row.FindControl("lbThanhTien");
                label.Text = string.Format("{0:0,0} VND",Sp.SoLuongNhap*TinhTienSanPham(Sp));

            }


        }

        //Tong tien tra (chua co tinh diem tich luy)
        private double TinhTien(List<shop_sanpham> listSP)
        {
            double ThanhTien = 0;
            foreach (shop_sanpham sp in listSP)
            {
                ThanhTien += (double)sp.SoLuongNhap * TinhTienSanPham(sp);
            }

            return ThanhTien;

        }

        private double TinhTienSanPham(shop_sanpham sp)
        {
            double CutdownRate = 0;

            foreach (shop_sanpham_khuyenmai spkm in sp.shop_sanpham_khuyenmais)
                if (spkm.shop_khuyenmai.MaKhuyenMai == 1)                //la giam gia
                    CutdownRate += (float)spkm.shop_khuyenmai.MucKhuyenMai / 100;

            return (double)sp.DonGia - (double)sp.DonGia * CutdownRate;
        }

        private double GetCutDownRateFromPoint()
        {
            if((Session["IsLogin"] ==null) || ((int)Session["IsLogin"] == 0))
            {
                return 0;
            }
            UserBUS UsrBUS = new UserBUS();
            string UserName = (string)Session["UserName"];
            aspnet_User User = UsrBUS.LayUserTheoTen(UserName);
            int DiemTL = (int)User.aspnet_Membership.DiemTichLuy;

            CauHinhBUS chBus = new CauHinhBUS();
            List<decimal> ProRates = chBus.LayDSGiaTriKhuyenMai();
            List<int> ProPoints = chBus.LayDSDieuKienKhuyenMai();

            for (int i = 2; i >= 0; i--)
                if (DiemTL >= ProPoints[i])
                    return (double)ProRates[i];

            return 0;


        }





    }
}
